import { defineStore } from 'pinia';
import { ref } from 'vue';
import { IUserInfo } from '../typings';

const initState = {
	nickname: '',
	avatar: 'https://breadbook.oss-cn-hangzhou.aliyuncs.com/static/teacher.png',
	school_id: 0,
	token: '',
	session_key: '',
	openid: '',
};

export const useUserStore = defineStore(
	'user',
	() => {
		const userInfo = ref<IUserInfo>({ ...initState });

		const setUserInfo = (val: IUserInfo) => {
			userInfo.value = val;
		};

		const clearUserInfo = () => {
			userInfo.value = undefined;
		};
		const reset = () => {
			userInfo.value = { ...initState };
		};

		return {
			userInfo,
			setUserInfo,
			clearUserInfo,
			reset,
		};
	},
	{
		persist: true,
	},
);
